#include "CThread.h"
#include "DBServer.h"
#include <libxml/parser.h>
#include <iostream>

using namespace std;

mysqlpp::Connection * CDBServer::m_con;

CDBServer::CDBServer()
{
	init();	
}

CDBServer::~CDBServer()
{


}

void CDBServer::Run()
{


}

bool CDBServer::AddExeucteStr(std::string strSql)
{

	return false;
}

bool CDBServer::init()
{
	xmlDocPtr doc;
	if((doc = xmlReadFile(XMLPATH,NULL,0)) == NULL)
	{
		perror("Load Configfile error!");
		return false;

	}
	else
	{
		perror("load configfile success~");	
		xmlNodePtr curNode =  xmlDocGetRootElement(doc);
		if (curNode == NULL)
		{
			perror("read configfile faile");
			return false;
		}
		xmlNodePtr childNode = curNode->xmlChildrenNode;

		if(childNode == NULL)
		{
			perror("read configfile faile!\n");
			return false;
		}
		
		xmlChar* szKey = NULL;
		while(childNode != NULL)
		{
			if ((!xmlStrcmp(childNode->name, (const xmlChar *)"host")))
			{
				szKey = xmlNodeGetContent(childNode);
				cout << "数据库的IP："<<szKey << endl; //得到node2的值并输出
				childNode = childNode->next;
				strcpy(this->dbhost,(const char*) szKey);
			}

			if((!xmlStrcmp(childNode->name, (const xmlChar*)"username")))
			{
				szKey = xmlNodeGetContent(childNode);
				cout << "用户名 :"<<szKey<<endl;
				childNode = childNode->next;
				strcpy(this->dbname, (const char*)szKey);
			}

			if((!xmlStrcmp(childNode->name, (const xmlChar*)"password")))
			{
				szKey = xmlNodeGetContent(childNode);
				cout << "密码 ：" <<szKey << endl;
				childNode = childNode->next;
				strcpy(this->dbpwd ,(const char*)szKey);
			}

			if((!xmlStrcmp(childNode->name, (const xmlChar*)"dbname")))
			{
				szKey = xmlNodeGetContent(childNode);
				cout << "数据库名字: "<<szKey <<endl;
				childNode = childNode->next;
				strcpy(this->dbasename , (const char*)szKey);
			
			}
			childNode = childNode->next;
		}		

	}

	m_con = new mysqlpp::Connection(false);

	if( m_con->connect(dbasename,dbhost, dbname, dbpwd))
	//if( m_con->connect("cq_db","localhost", "root", "firefly"))
	{
		cout<<" connect dbserver success~!"<<endl;	
	}
	else
	{
		cout<<" connection dbserver fail~!"<<endl;
	}








	return true;
}
